package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompUserRoleInfoMapper;
import cc.rengu.igas.bomp.common.entity.UserRoleInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户权限操作实现.
 */
public class BompUserRoleInfoMapperImpl implements BompUserRoleInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);


    @Override
    public List<UserRoleInfo> selectBompUserRoleInfoByRoleType(String instId, String roleType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompUserRoleInfoListByRoleType");
        UserRoleInfo userRoleInfo = new UserRoleInfo();
        userRoleInfo.setInstId(instId);
        userRoleInfo.setRoleType(roleType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userRoleInfo, UserRoleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据角色类型<{}>获取用户角色信息失败,iReturnCode:<{}>", roleType);
            return null;
        }
        return objectList.stream().map(item -> (UserRoleInfo) item).collect(Collectors.toList());
    }

    @Override
    public UserRoleInfo selectBompUserRoleInfoByRoleTypeAndTxnNum(String instId, String roleType, String txnNum, String appType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompUserRoleInfoByRoleTypeAndTxnNum");
        UserRoleInfo userRoleInfo = new UserRoleInfo();
        userRoleInfo.setInstId(instId);
        //角色
        userRoleInfo.setRoleType(roleType);
        //交易码
        userRoleInfo.setFuncDroitId(txnNum);
        if ("WECHAT".equals(appType)) {
            appType = "H5";
        }
        userRoleInfo.setAppType(appType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userRoleInfo, UserRoleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据角色类型<{}>,appType:{},txnNum:{},获取用户角色信息失败.", roleType, appType, txnNum);
            return null;
        }

        List<UserRoleInfo> userRoleInfoList = objectList.stream().map(item -> (UserRoleInfo) item).collect(Collectors.toList());
        return userRoleInfoList.get(0);
    }
}
